$(document).ready(function () {
	$("#search-btn").on("click", function(e) {
		e.preventDefault();
		fetchBookByBID($("#BID").val());
    });

    $("#confirm-book-arraived-btn").on("click", function(e) {
        $.ajax({
            type: "POST",
            url: "../Handle/AdminHandlerService.ashx?method=Confirm_One_Book_Arrived",
            data: {BorrowID: $("#borrowId").val(), ID: $("#j-book-id").val(), JUID: $("#juid").val(), BID: $("#bid").val(), CUID: $("#cuid").val(), Bdate: $("#bdate").val() },
            dataType: "json",
            success: function (data) {
                //console.log(data);
                if(1 == data.MessageType) {
                    //alert(data.MessageContent);
                    $("#BID").val("");
                    $("#BID").focus();
                    
                    $('#search-result').html("<h4>" + data.MessageContent + "</h4>");
                    $("#result-area").addClass("hidden");
                } else if (0 == data.MessageType) {
                    //alert(data.MessageContent);
                    $('#search-result').html("<h4>" + data.MessageContent + "</h4>");
                    $("#result-area").addClass("hidden");
                }
            }
        });
    });
});

function searchValidate() {
    return $("#search-form").validate({
        errorClass: "warning",
        errorContainer: "div.error",
        errorLabelContainer: $("#search-form div.error"),
        wrapper: "p",
        rules: {
            BID: {
                required: true,
                minlength: 4,
                maxlength: 12,
            },
        },
        messages: {
            BID: {
                required: "图书编号不能为空",
                minlength: "图书编号长度要大于等于4",
                maxlength: "图书编号长度要小于等于12",
            },
        }
    }).form();
}

function fetchBookByBID(BID) {
	if (searchValidate()) {
            $.ajax({
                type: "GET",
                url: "../Handle/AdminHandlerService.ashx?method=Ad_Search_Book_From_BID",
                data: {BID: BID },
                dataType: "json",
                success: function (data) {
                    $('#search-result').html("");
                    $("#result-area").addClass("hidden");
                    console.log(data);
                    if (0 == data.MessageType) {
                        $('#search-result').html("<p>该编号图书未处于流通状态，或者没有此书。</p>");
                    } else {
                        $("#result-area").removeClass("hidden");
                        var resultData = data.rows[0];
                        $("#borrow-student-name").text(resultData["借阅学生"]);
                        $("#borrow-student-grade").text(resultData["借阅人年级"]);
                        $("#borrow-student-class").text(resultData["借阅人班级"]);
                        $("#borrow-student-phone").text(resultData["借阅人电话"]);

                        $("#lend-student-name").text(resultData["持有学生"]);
                        $("#lend-student-grade").text(resultData["持有人年级"]);
                        $("#lend-student-class").text(resultData["持有人班级"]);
                        $("#lend-student-phone").text(resultData["持有人电话"]);

                        $("#result-book-id").text("图书编号：" + resultData["BID"]);

                        $("#book-img").attr("src", resultData["Pname"]);
                        $("#book-id").text(resultData["BID"]);
                        $("#book-author").text(resultData["Author"]);
                        $("#book-press").text(resultData["Press"]);
                        $("#book-pym").text(resultData["PYM"]);
                        $("#book-price").text(resultData["Price"] + "元");
                        $("#book-isbn").text(resultData["ISBN"]);
                        $("#book-cid").text(resultData["cname"]);

                        $("#book-circulation-status").text(resultData["流程方向"]);

                        $("#j-book-id").val(resultData["ID"]);
                        $("#juid").val(resultData["JUID"]);
                        $("#cuid").val(resultData["CUID"]);
                        $("#bid").val(resultData["BID"]);
                        $("#bdate").val(resultData["Bdate"]);
                        $("#borrowId").val(resultData["BorrowID"]);

                        if(resultData["Cstatus"] == 2) {
                            $("#confirm-book-arraived-btn").addClass("diable");
                        }
                    }


                    // if(data.ReturnValue == 99) {
                    // 书号 作者 出版社 出版时间  定价ISBN 分类  所有者
                		
                		// return;
                    // }
                    // $('#search-book-list').html("<p>非常抱歉！没有你要找的书！</p>");
                    // $('#search-book-list').html(getBookListOutputHtml(data.rows));

                }
            });
        }
}